/**
 * Created by Ysssssss on 21/1/14.
 */
import { put, takeEvery } from 'redux-saga/effects'
import apiType, {
    GET_BRAND_LIST,
    EMPLOYEE_BRAND_LOGIN,
    GET_USER_INFO,
    GET_USER_INFO_IN_STORE,
    GET_WX_QR_CODE,
    POST_TICKET_LOGIN,
    POST_TICKET_REGISTER,
    SEND_VERIFY_CODE,
    UPDATE_USER_INFO, EDIT_BRAND, CREATE_BRAND, GET_BRAND_INFO
} from "@constants/ActionTypes";
import {getAxios, postAxios, putAxios} from "@utils/api";
import {reformedRoute} from "@routes/config";
import {getBilinInfo} from "@utils/localStorage";

/**
 * 获取用户信息
 * @returns {{type: string}}
 */
export const getUserInfo = () => ({
    type: apiType(GET_USER_INFO).request
})

export const getUserInfoInStore = (callback) => ({
    type: apiType(GET_USER_INFO_IN_STORE).request,
    callback
})

/**
 * 获取用户品牌列表
 * @returns {{type: string}}
 */
export const getBrandList = () => ({
    type: apiType(GET_BRAND_LIST).request,
})

/**
 * 获取用户品牌详情
 * @returns {{type: string}}
 */
export const getBrandInfo = (callback) => ({
    type: apiType(GET_BRAND_INFO).request,
    callback
})

/**
 * 创建品牌
 * @param logo
 * @param name
 * @param desc
 * @param callback
 * @returns {{callback: *, type: string, params: {name: *, logo: *, desc: *}}}
 */
export const createBrand = ({ logo, name, desc }, callback) => ({
    type: apiType(CREATE_BRAND).request,
    params: {
        logo,
        name,
        desc,
        origin: "web"
    },
    callback
})

export const editBrand = ({ brandId, logo, name, desc }, callback) => ({
    type: apiType(EDIT_BRAND).request,
    params: {
        brandId,
        logo,
        name,
        desc,
    },
    callback
})
/**
 * 员工品牌登录
 * @param brandId
 * @param callback
 * @returns {{type: string, params: {brandId: *}}}
 */
export const employeeBrandLogin = ({ brandId }, callback) => ({
    type: apiType(EMPLOYEE_BRAND_LOGIN).request,
    params: {
        brandId
    },
    callback,
})
// 获取微信登录二维码
export const getWxQrCode = (callback) => ({
    type: apiType(GET_WX_QR_CODE).request,
    callback
})
// 通过ticket轮训登录
export const ticketAutoLogin = (params,callback) => ({
    type: apiType(POST_TICKET_LOGIN).request,
    params,
    callback
})
// 扫码之后注册
export const ticketRegister = (params,callback) => ({
    type: apiType(POST_TICKET_REGISTER).request,
    params,
    callback
})
// 发送验证码
export const sendVerifyCode = (params,callback) => ({
    type: apiType(SEND_VERIFY_CODE).request,
    params,
    callback
})
// 更新用户信息
export const updateUserInfo = (params,callback) => ({
    type: apiType(UPDATE_USER_INFO).request,
    params,
    callback
})

const sagas = {
    * [apiType(GET_USER_INFO).request] () {
        try {
            const response = yield getAxios('/account/api/sso/v1/accounts/info')
            yield put({ type: apiType(GET_USER_INFO).success, response });
        } catch (e) {
            yield put({ type: apiType(GET_USER_INFO).failure });
        }
    },
    * [apiType(GET_BRAND_LIST).request] () {
        try {
            const response = yield getAxios('/admin/api/employee/v1/related/brands')
            yield put({
                type: apiType(GET_BRAND_LIST).success,
                response
            })
        } catch (e) {
            yield put({ type: apiType(GET_BRAND_LIST).failure });
        }
    },
    * [apiType(GET_BRAND_INFO).request] ({ callback }) {
        try {
            const response = yield getAxios('/admin/api/brand/v1/info')
            yield put({
                type: apiType(GET_BRAND_INFO).success,
                response
            })
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(GET_BRAND_INFO).failure });
        }
    },
    * [apiType(CREATE_BRAND).request] ({ params, callback }) {
        try {
            const response = yield postAxios('/admin/api/brand/v1/create', params)
            yield put({
                type: apiType(CREATE_BRAND).success,
                response
            })
            callback && callback()
        } catch (e) {
            yield put({ type: apiType(CREATE_BRAND).failure });
        }
    },
    * [apiType(EDIT_BRAND).request] ({ params, callback }) {
        try {
            const response = yield putAxios('/admin/api/brand/v1/update', params)
            yield put({
                type: apiType(EDIT_BRAND).success,
                response
            })
            callback && callback()
        } catch (e) {
            yield put({ type: apiType(EDIT_BRAND).failure });
        }
    },
    * [apiType(EMPLOYEE_BRAND_LOGIN).request] ({ params, callback }) {
        try {
            const response = yield postAxios('/admin/api/employee/v1/login', params)
            callback && callback(response)
        } catch (e) {
            console.log('EMPLOYEE_BRAND_LOGIN', e)
        }
    },
    * [apiType(GET_USER_INFO_IN_STORE).request]  ({ callback }) {
        try {
            const userResponse = yield getAxios('/admin/api/employee/v1/op/info')
            const brandResponse = yield getAxios('/admin/api/brand/v1/info')
            const response = {
                ...userResponse,
                ...brandResponse,
            }
            const permissionTag = {
                bizType: response.mall_opened ? response.biz_type : null,
                isAdmin: response.is_super_admin || response.is_admin,
                isBrand: getBilinInfo('storeId') === 0
            }
            reformedRoute(permissionTag)
            yield put({
                type: apiType(GET_USER_INFO_IN_STORE).success,
                response,
                payload: {
                    permissionTag
                }
            });
            callback && callback()
        } catch (e) {
            yield put({ type: apiType(GET_USER_INFO_IN_STORE).failure});
            console.log('res error', e)
        }
    },
    * [apiType(GET_WX_QR_CODE).request]  ({ callback }) {
        try {
            const response = yield getAxios('/account/api/sso/v1/mp/qrcode')
            yield put({ type: apiType(GET_WX_QR_CODE).success, response });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(GET_WX_QR_CODE).failure});
            console.log('res error', e)
        }
    },
    * [apiType(POST_TICKET_LOGIN).request]  ({ params, callback }) {
        try {
            const response = yield getAxios(`/account/api/sso/v1/login/${params.ticket}`)
            yield put({ type: apiType(POST_TICKET_LOGIN).success, response });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(POST_TICKET_LOGIN).failure});
            console.log('res error', e)
        }
    },
    * [apiType(POST_TICKET_REGISTER).request]  ({ params, callback }) {
        try {
            const response = yield postAxios(`/account/api/sso/v1/ticket/register`,params)
            yield put({ type: apiType(POST_TICKET_REGISTER).success, response });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(POST_TICKET_REGISTER).failure});
            console.log('res error', e)
        }
    },
    * [apiType(SEND_VERIFY_CODE).request]  ({ params, callback }) {
        try {
            const response = yield postAxios(`/account/api/sso/v1/mobile/code/send`,params)
            yield put({ type: apiType(SEND_VERIFY_CODE).success, response });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(SEND_VERIFY_CODE).failure});
            console.log('res error', e)
        }
    },
    * [apiType(UPDATE_USER_INFO).request]  ({ params, callback }) {
        try {
            const response = yield putAxios(`/account/api/sso/v1/accounts/info`,params)
            yield put({ type: apiType(UPDATE_USER_INFO).success, response });
            callback && callback()
        } catch (e) {
            yield put({ type: apiType(UPDATE_USER_INFO).failure});
            console.log('res error', e)
        }
    },
}
export default Object.keys(sagas).map(item => {
    return function * s() {
        yield takeEvery(item, function *(args) {
            try {
                yield sagas[item](args)
            } catch (e) {
                console.log(e)
            }
        })
    }
})